package com.summer.data.dialect.sqlite;

import com.summer.data.Jsql;
import com.summer.data.dialect.AbstractDialect;
import com.summer.data.dialect.DatabaseProductID;
import com.summer.data.parameter.BlobParameter;
import com.summer.data.parameter.ClobParameter;
import com.summer.data.rs.extractor.KeysExtractor;
import com.summer.data.table.BaseTable;

import java.math.BigDecimal;
import java.sql.JDBCType;
import java.sql.Timestamp;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * SQLite supports only TRANSACTION_SERIALIZABLE and TRANSACTION_READ_UNCOMMITTED.
 * @author 345480567@qq.com
 */
public class SqliteDialect extends AbstractDialect {

    public static DatabaseProductID SQLITE_ID = new DatabaseProductID("DEFAULT", 1);

    // https://dev.mysql.com/doc/refman/8.0/en/data-types.html

    private static final Map<Class<?>, JDBCType> JAVA_2_JDBC = new HashMap<Class<?>, JDBCType>(){{
        put(Boolean.class, JDBCType.BOOLEAN);
        put(Byte.class, JDBCType.BIT);

        put(Short.class, JDBCType.SMALLINT);
        put(Integer.class, JDBCType.INTEGER);
        put(Long.class, JDBCType.BIGINT);

        put(Float.class, JDBCType.FLOAT);
        put(Double.class, JDBCType.DOUBLE);

        put(BigDecimal.class, JDBCType.DECIMAL);

        put(String.class, JDBCType.VARCHAR);

        // 日期是最复杂的
        put(LocalDate.class, JDBCType.DATE);
        put(LocalTime.class, JDBCType.TIME_WITH_TIMEZONE);
        put(LocalDateTime.class, JDBCType.TIME);

        put(Date.class, JDBCType.DATE);
        put(java.sql.Date.class, JDBCType.DATE);

        put(Timestamp.class, JDBCType.TIMESTAMP);

        put(ClobParameter.class, JDBCType.CLOB);
        put(BlobParameter.class, JDBCType.BLOB);

    }};

    @Override
    public String getPageSql(String string, Integer offset, Integer pageSize) {
        return String.format("%s LIMIT %d, %d ", string, offset, pageSize);
    }


    @Override
    public Map<Class<?>, JDBCType> getJDBCTypeMap() {
        return JAVA_2_JDBC;
    }

    @Override
    public KeysExtractor buildKeysExtractor(BaseTable table) {
        // todo:
        return new KeysExtractor();
    }
}

